package com.ruoyi.web.controller.business;


import com.ruoyi.business.domain.CivilServants;
import com.ruoyi.business.domain.CivilServantsList;
import com.ruoyi.business.domain.vo.CivilServantsListVo;
import com.ruoyi.business.service.CivilServantsListService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/civilServantsList")
@Api(tags = {"机关公务员正负清单控制器"})
public class CivilServantsListController extends BaseController {

    @Autowired
    CivilServantsListService civilServantsListService;

    @Autowired
    private ServerConfig serverConfig;



    @GetMapping
    @ApiOperation("查询机关公务员正负清单")

    public TableDataInfo getList(@ApiParam(value = "机关公务员id") Long civilServantsId,
                                 @ApiParam(value = "正反清单标识（0代表正面 2代表负面）") String prosCons) throws Exception {
        startPage();
        List<CivilServantsListVo> list = civilServantsListService.getList(civilServantsId, prosCons);
        return getDataTable(list);
    }

    @DeleteMapping
    @ApiOperation("删除机关公务员正负清单控制器")
    public AjaxResult delete(@ApiParam(value = "清单id") Long id)  {
        civilServantsListService.delete(id);
        return success();
    }

    @DeleteMapping("/{ids}")
    @ApiOperation("批量删除机关公务员正负清单")
    @PreAuthorize("@ss.hasPermi('jiguandeatil:remove')")
    public AjaxResult delete(@ApiParam(value = "清单id") @PathVariable List<Long> ids) {
        for (Long id : ids) {
            civilServantsListService.delete(id);
        }
        return success();
    }

    @PostMapping
    @ApiOperation("新增机关公务员正负清单")
    @PreAuthorize("@ss.hasPermi('jiguandeatil:add')")
    public AjaxResult add(@RequestBody CivilServantsList civilServantsList) throws Exception {
//        String filePath = RuoYiConfig.getUploadPath();
//        // 上传并返回新文件名称
//        String fileName = FileUploadUtils.upload(filePath, file);
//        String url = serverConfig.getUrl() + fileName;
//        AjaxResult ajax = AjaxResult.success();
//        ajax.put("url", url);
//        civilServantsList.setLocation(url);
        civilServantsListService.add(civilServantsList);
        return success();
    }


    @PutMapping
    @ApiOperation("修改机关公务员正负清单控制器")
    @PreAuthorize("@ss.hasPermi('jiguandeatil:edit')")
    public AjaxResult update(@RequestBody CivilServantsList civilServantsList) throws Exception {
//        String filePath = RuoYiConfig.getUploadPath();
//        // 上传并返回新文件名称
//        String fileName = FileUploadUtils.upload(filePath, file);
//        String url = serverConfig.getUrl() + fileName;
//        AjaxResult ajax = AjaxResult.success();
//        ajax.put("url", url);
//        civilServantsList.setLocation(url);
        civilServantsListService.update(civilServantsList);
        return success();
    }

}
